package com.guoliang.eduService.controller;

import com.guoliang.commonUtils.CommonResult;
import com.guoliang.eduService.entity.EduVideo;
import com.guoliang.eduService.service.EduVideoService;
import com.guoliang.eduService.service.VodClient;
import com.guoliang.service.exceptionHandler.MyException;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author leo
 * @since 2023-01-18
 */
@RestController
@RequestMapping("/eduService/video")
public class EduVideoController {
    @Autowired
    private EduVideoService eduVideoService;
    @Autowired
    private VodClient vodClient;
    @ApiOperation("添加小节")
    @PostMapping
    public CommonResult addVideo(@RequestBody EduVideo eduVideo){
        eduVideoService.save(eduVideo);
        return CommonResult.ok();
    }
    @ApiOperation("删除小节")
    @DeleteMapping("{id}")
    public CommonResult deleteVideo(@PathVariable("id")String id){
        String videoSourceId = eduVideoService.getById(id).getVideoSourceId();
        if (!StringUtils.isEmpty(videoSourceId)){
            CommonResult commonResult = vodClient.deleteVod(videoSourceId);
            if (commonResult.getCode()==20001){
                throw new MyException(20001,"删除失败");
            }
        }
        eduVideoService.removeById(id);
        return CommonResult.ok();
    }
    @ApiOperation("修改小节")
    @PutMapping
    public CommonResult updateVideo(@RequestBody EduVideo eduVideo){
        eduVideoService.updateById(eduVideo);
        return CommonResult.ok();
    }
    @ApiOperation("根据id获取")
    @GetMapping("getById/{id}")
    public CommonResult getVideoById(@PathVariable("id")String id){
        EduVideo eduVideo = eduVideoService.getById(id);
        return CommonResult.ok().data("video",eduVideo);
    }

}
